package com.easygroup.ngarihealth.plugin

import com.android.build.gradle.AppExtension
import org.gradle.api.GradleException
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.UnknownTaskException

/**
 * Created by fuxiaohua on 2021/5/10 0010.
 */
class EasyModulePlugin implements Plugin<Project> {

    @Override
    void apply(Project target) {
        target.extensions.create("easymodule", EasyModuleExtension)
        def type = target.extensions.findByType(AppExtension)
        if (type instanceof AppExtension) {
            applyApplication(target, type)
        }
    }

    private static void applyApplication(Project target, def android) {
        android.registerTransform(new EasyModuleTransform(target, android.applicationVariants))
        // throw an exception in instant run mode
        android.applicationVariants.all { variant ->
            def variantName = variant.name.capitalize()
            try {
                def instantRunTask = target.tasks.getByName("transformClassesWithInstantRunFor${variantName}")
                if (instantRunTask) {
                    throw new GradleException(
                            "StringFog does not support instant run mode, please trigger build"
                                    + " by assemble${variantName} or disable instant run"
                                    + " in 'File->Settings...'."
                    )
                }
            } catch (UnknownTaskException ignored) {
                // Not in instant run mode, continue.
            }
        }
    }
}